package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.bean.PrefStrategyBean;
import com.koron.css2.baseConfig.bean.TradeClassifyBean;
import com.koron.css2.baseConfig.mapper.*;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.clientCenter.bean.*;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.OpenUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.MeterService;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DistrictMapper;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 开户详情信息
 * 
 * @author 甄玉晴
 *
 */
public class OpenAccQuery implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			MessageBean<OpenUserBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功",
					OpenUserBean.class);
			UserOpenBean userOpen = JsonUtils.objectToPojo(req.getData(), UserOpenBean.class);
			OpenUserMapper mapper = factory.getMapper(OpenUserMapper.class);
			UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
			AccountInfoMapper accountMapper = factory.getMapper(AccountInfoMapper.class);
			CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
			UserOpenBean userOpenMessage = mapper.getUserOpenListInfoByDetailId(userOpen.getId());// 获取开户记录信息
			AccountInfoBean accountInfo = accountMapper.getAccountInfoById(userOpenMessage.getUserAccountTempId(),AccountInfoBean.ACCOUNTTEMP);// 获取临时账户信息
			CtmInfoBean ctmInfo = ctmMapper.getCtmInfoById(userOpenMessage.getUserCtmTempId(), CtmInfoBean.CTMTEMP);// 获取临时客户信息
			Map<String, String> CTT = Tools.mapDicByCode(factory, "CTT");
			Map<String, String> CDL = Tools.mapDicByCode(factory, "CDL");
			Map<String, String> IDT = Tools.mapDicByCode(factory, "IDT");
			
			PubAdminAreaMapper pmapper = factory.getMapper(PubAdminAreaMapper.class,"_default");
			
			ctmInfo.setCtmTypeName(CTT.get(ctmInfo.getCtmType()));
			ctmInfo.setCreditLevelName(CDL.get(ctmInfo.getCreditLevel()));
			ctmInfo.setCertTypeName(IDT.get(ctmInfo.getCertType()));
			
			if(!StringUtils.isEmpty(ctmInfo.getClientAddr())) {
				String area = pmapper.selectFullNameById(ctmInfo.getClientAddr());
				ctmInfo.setClientAddr(area);
			}
			if(StringUtils.isNotBlank(accountInfo.getVatFlag()) && "1".equals(accountInfo.getVatFlag())) {
				accountInfo.setVatFlagName("是");
			}else {
				accountInfo.setVatFlagName("否");
			}
			//账户类型
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getAccountType())) {
				accountInfo.setAccountTypeName(Tools.getDicNameByCodeAndValue(factory, "ACT", accountInfo.getAccountType()));
			}
			//缴费方式
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getPayWay())) {
				accountInfo.setPayWayName(Tools.getDicNameByCodeAndValue(factory, "BPW", accountInfo.getPayWay()));
			}
			//发票类型
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getInvoiceType())) {
				accountInfo.setInvoiceTypeName(Tools.getDicNameByCodeAndValue(factory, "BBT", accountInfo.getInvoiceType()));
			}
			//投递方式
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getDeliverWay())) {
				accountInfo.setDeliverWayName(Tools.getDicNameByCodeAndValue(factory, "SDW", accountInfo.getDeliverWay()));
			}
			UserBean openUserInfo = userMapper.getUserInfoById(userOpenMessage.getUserInfoTempId(), UserBean.USERTEMP);// 获取临时用户信息
			
			if(!StringUtils.isEmpty(openUserInfo.getClientContractAddr())) {
				openUserInfo.setClientContractAddrId(openUserInfo.getClientContractAddr());
				String area = pmapper.selectFullNameById(openUserInfo.getClientContractAddr());
				openUserInfo.setClientContractAddr(area);
			}
			//抄表周期
			if (openUserInfo != null && StringUtils.isNotBlank(openUserInfo.getMeterReadingCycle())) {
				openUserInfo.setMeterReadingCycleName(Tools.getDicNameByCodeAndValue(factory, "RTP", openUserInfo.getMeterReadingCycle()));
			}
			//水表类型
			if (openUserInfo != null && StringUtils.isNotBlank(openUserInfo.getMeterType())) {
				openUserInfo.setMeterTypeName(Tools.getDicNameByCodeAndValue(factory, "MMT", openUserInfo.getMeterType()));
			}
			// setupMeterLocation->setupMeterLocationName
			if (openUserInfo != null && StringUtils.isNotBlank(openUserInfo.getSetupMeterLocation())) {
				openUserInfo.setSetupMeterLocationName(Tools.getDicNameByCodeAndValue(factory, "CTL", openUserInfo.getSetupMeterLocation()));
			}
			//表井位置
			if (openUserInfo != null && StringUtils.isNotBlank(openUserInfo.getMeterBjwz())) {
				openUserInfo.setMeterBjwzName(Tools.getDicNameByCodeAndValue(factory, "BJWZ", openUserInfo.getMeterBjwz()));
			}
			//表井形式
			if (openUserInfo != null && StringUtils.isNotBlank(openUserInfo.getMeterBjxs())) {
				openUserInfo.setMeterBjxsName(Tools.getDicNameByCodeAndValue(factory, "BJXS", openUserInfo.getMeterBjxs()));
			}
			//表井模式
			if (openUserInfo != null && StringUtils.isNotBlank(openUserInfo.getMeterBjms())) {
				openUserInfo.setMeterBjmsName(Tools.getDicNameByCodeAndValue(factory, "BJMS", openUserInfo.getMeterBjms()));
			}
			//营业所
			BusinessAbodeMapper Businessmapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
			//获取下拉框
			List<SelectVO> list = Businessmapper.businessAbodeSelect(userInfo.getCurWaterCode());
			for (SelectVO selectVO : list) {
				if(selectVO.getId().equals(openUserInfo.getBusinessAbode())) {
					openUserInfo.setBusinessAbodeName(selectVO.getName());
				}
			}
			// 获取行政区域
			if (!StringUtils.isEmpty(openUserInfo.getAdminArea())) {
				DistrictMapper districtMapper = factory.getMapper(DistrictMapper.class);
				openUserInfo.setAdminAreaName(districtMapper.findDistrictById(openUserInfo.getAdminArea()).getSname());
			}
			// 获取营业区域
			if (!StringUtils.isEmpty(openUserInfo.getBusinessArea())) {
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				openUserInfo.setBusinessAreaName(
						businessAreaMapper.findBusinessAreaById(openUserInfo.getBusinessArea(), userInfo.getCurWaterCode()));
			}
//			// 获取用水类型
//			if (!StringUtils.isBlank(openUserInfo.getUseWaterType())) {
//				WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
//				openUserInfo.setUseWaterTypeName(waterTypeMapper.findWaterTypeById(openUserInfo.getUseWaterType()).getWaterTypeName());
//			}
			// 获取优惠策略名称
			if (!StringUtils.isEmpty(openUserInfo.getPrefStrategyId())) {
				PrefStrategyMapper prefStrategyMapper = factory.getMapper(PrefStrategyMapper.class);
				PrefStrategyBean prefStrategy = prefStrategyMapper.selectPrefStrategyById(openUserInfo.getPrefStrategyId());
				openUserInfo.setPrefStrategyName(prefStrategy==null?"":prefStrategy.getStrategyName());
			}
			//水表用途
			if (StringUtils.isNotBlank(openUserInfo.getMeterUse())) {
				openUserInfo.setMeterUseName(Tools.getDicNameByCodeAndValue(factory, "CMC", openUserInfo.getMeterUse()));
			}
			//用水对象
			if (StringUtils.isNotBlank(openUserInfo.getUseWaterObject())) {
				openUserInfo.setUseWaterObjectName(Tools.getDicNameByCodeAndValue(factory, "UWS", openUserInfo.getUseWaterObject()));
			}
			// 获取行业分类名称
			if (!StringUtils.isEmpty(openUserInfo.getTradeClassify())) {
				TradeClassifyMapper tradeMapper = factory.getMapper(TradeClassifyMapper.class);
				TradeClassifyBean tradeClassifyBean=tradeMapper.selectTradeClassifyById(openUserInfo.getTradeClassify());
				openUserInfo.setTradeClassifyName(tradeClassifyBean==null?"":tradeClassifyBean.getTradeName());
			}
			BankMapper bankeMapper = factory.getMapper(BankMapper.class);
			// 获取开户行名称
			if (!StringUtils.isBlank(accountInfo.getOpenAccountBank())) {
				BankBean bankName = bankeMapper.findBankById(accountInfo.getOpenAccountBank());
				accountInfo.setOpenAccountBankName(bankName==null?"":bankName.getBankName());

			}
			// 获取购买方开户行名称
			if (!StringUtils.isBlank(accountInfo.getInvoiceBank())) {
				BankBean bankName = bankeMapper.findBankById(accountInfo.getInvoiceBank());
				accountInfo.setInvoiceBankName(bankName==null?"":bankName.getBankName());
			}
			
			MsInfoDto msInfo=null;
			if(!StringUtils.isEmpty(openUserInfo.getMeterNo())) {
				// 获取水表信息
				MeterService meterSrvice = new MeterService();
				msInfo = ADOConnection.runTask("_default", meterSrvice, "getMsInfoByNo", MsInfoDto.class,
						openUserInfo.getMeterNo(),userInfo.getCurWaterCode());
			}
			OpenUserBean bean = new OpenUserBean();
			bean.setUserOpenInfo(userOpenMessage);
			bean.setAccountInfo(accountInfo);
			bean.setCtmInfo(ctmInfo);
			bean.setUserInfo(openUserInfo);
			bean.setMsInfo(msInfo);
			info.setData(bean);
			return info;
		} catch (Exception e) {
			logger.error("客户管理列表查询失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
